home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1997 #1 / Amiga Plus CD - 1997 - No. 01.iso / pd / programmierung / mesa-1.2.8 / src-tk / awindow.c next >
Encoding:
C/C++ Source or Header  |  1996-05-27  |  12.6 KB  |  591 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  26.  */
  27.  
  28. /* Mesa tkAmiga by Stefan Z (d94sz@efd.lth.se)*/
  29. /*
  30. History:
  31.  
  32. 1.0 960315 Now almost evetything is implemented
  33. 1.1 960425 Fixed problem with double closeclicks (Thanx to Daniel Jönsson)
  34.  
  35. TODO:
  36. Exposefunc
  37. Middle & Righmousebutton
  38.  
  39.  
  40. */
  41.  
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45.  
  46. #include <intuition/intuition.h>
  47. #include <proto/exec.h>
  48. #include <proto/intuition.h>
  49. #include <proto/graphics.h>
  50.  
  51.  
  52. #include "gltk.h"
  53. #include "gl/Amigamesa.h"
  54.  
  55. #define static
  56.  
  57. #if defined(__cplusplus) || defined(c_plusplus)
  58. #define class c_class
  59. #endif
  60.  
  61. #if DBG
  62. #define TKASSERT(x)                                     \
  63. if ( !(x) ) {                                           \
  64.     PrintMessage("%s(%d) Assertion failed %s\n",        \
  65.         __FILE__, __LINE__, #x);                        \
  66. }
  67. #else
  68. #define TKASSERT(x)
  69. #endif  /* DBG */
  70.  
  71. /******************************************************************************/
  72.  
  73. struct wnd 
  74.     {
  75.     int x, y, width, height;
  76.     GLenum type;
  77.     struct amigamesa_context *context;
  78.     };
  79.  
  80. struct wnd win={0,0,100,100,TK_INDEX,NULL};
  81.  
  82.  
  83.  
  84.  
  85.  
  86. /*
  87. struct Library *IntuitionBase;
  88. struct Library *GfxBase;
  89. */
  90. struct Screen *screen = NULL;
  91. struct Window *tkhwnd = NULL;
  92.  
  93.  
  94.  
  95.  
  96. GLboolean tkPopupEnable = TRUE;
  97.  
  98. /* Fixed palette support.  */
  99. /*
  100. #define BLACK   PALETTERGB(0,0,0)
  101. #define WHITE   PALETTERGB(255,255,255)
  102. #define NUM_STATIC_COLORS   (COLOR_BTNHIGHLIGHT - COLOR_SCROLLBAR + 1)
  103. */
  104. static void (*ExposeFunc)(int, int)              = NULL;
  105. static void (*ReshapeFunc)(GLsizei, GLsizei)     = NULL;
  106. static void (*DisplayFunc)(void)                 = NULL;
  107. static GLenum (*KeyDownFunc)(int, GLenum)        = NULL;
  108. static GLenum (*MouseDownFunc)(int, int, GLenum) = NULL;
  109. static GLenum (*MouseUpFunc)(int, int, GLenum)   = NULL;
  110. static GLenum (*MouseMoveFunc)(int, int, GLenum) = NULL;
  111. static void (*IdleFunc)(void)                    = NULL;
  112.  
  113. /*
  114.  *  Prototypes for the debugging functions go here
  115.  */
  116.  
  117. #define DBGFUNC 0
  118. #if DBGFUNC
  119.  
  120. static void DbgPrintf( const char *Format, ... );
  121. static void pwi( void );
  122. static void pwr(RECT *pr);
  123. /*static void ShowPixelFormat(HDC hdc);*/
  124.  
  125. #endif
  126.  
  127.  
  128. float tkRGBMap[8][3] = 
  129.     {
  130.     {    0, 0, 0 },
  131.     {    1, 0, 0 },
  132.     {    0, 1, 0 },
  133.     {    1, 1, 0 },
  134.     {    0, 0, 1 },
  135.     {    1, 0, 1 },
  136.     {    0, 1, 1 },
  137.     {    1, 1, 1 }
  138.     };
  139.  
  140. int atk_setIDCMPs(void)
  141.     {
  142.     int mask;
  143.     mask=IDCMP_CLOSEWINDOW;
  144.     if (ReshapeFunc)
  145.         mask |=IDCMP_NEWSIZE;
  146.     if (KeyDownFunc)
  147.         mask |=(IDCMP_RAWKEY | IDCMP_VANILLAKEY);
  148.     if (MouseDownFunc||MouseUpFunc)
  149.         mask |=IDCMP_MOUSEBUTTONS;
  150.     if (MouseMoveFunc)
  151.         mask |=    IDCMP_MOUSEMOVE;
  152.  
  153.     return(mask);
  154.     }
  155.  
  156. /* TODO (*ExposeFunc)(int, int)   */
  157.  
  158. void atk_modyfyIDCMP(void)
  159.     {
  160.     if(tkhwnd)
  161.         {
  162.         ModifyIDCMP( tkhwnd, atk_setIDCMPs() );
  163.         }
  164.     }
  165.  
  166.  
  167. int atk_FixKeyRAW(char c)
  168.     {
  169.     int key;
  170.     switch (c) 
  171.         {
  172.         /*
  173.       case XK_Return:     key = TK_RETURN;    break;
  174.       case XK_Escape:     key = TK_ESCAPE;    break;
  175.         */
  176.       case 'O':        key = TK_LEFT;        break;
  177.       case 'L':        key = TK_UP;            break;
  178.       case 'N':      key = TK_RIGHT;        break;
  179.       case 'M':        key = TK_DOWN;        break;
  180.       default:         key = GL_FALSE;        break;
  181.     }
  182.     return(key);
  183.     }
  184.  
  185.  
  186. /***************************************************************
  187.  *                                                             *
  188.  *  Exported Functions go here                                 *
  189.  *                                                             *
  190.  ***************************************************************/
  191.  
  192.  
  193. void tkNewCursor(GLint id, GLubyte *shapeBuf, GLubyte *maskBuf, GLenum fgColor,
  194.      GLenum bgColor, GLint hotX, GLint hotY)
  195. {
  196. /* cursor.c */ 
  197. }
  198.  
  199. void tkSetCursor(GLint id)
  200. {
  201. /* cursor.s*/
  202. }
  203.  
  204. void tkErrorPopups(GLboolean bEnable)
  205. {
  206.     tkPopupEnable = bEnable;
  207. }
  208.  
  209. void tkCloseWindow(void)
  210.     {
  211.     if (win.context) 
  212.         {
  213.         AmigaMesaDestroyContext(win.context);
  214.         win.context=NULL;
  215.         }
  216.     if (tkhwnd)
  217.         {
  218.         CloseWindow(tkhwnd);
  219.         tkhwnd=NULL;
  220.         }
  221.     if (GfxBase)
  222.         {
  223.         CloseLibrary(GfxBase);
  224.         GfxBase=NULL;
  225.         }
  226.     if (IntuitionBase)
  227.         {
  228.         CloseLibrary(IntuitionBase);
  229.         IntuitionBase=NULL;
  230.         }
  231.     }
  232.  
  233.  
  234.  
  235.  
  236. void tkExec(void)
  237.     {
  238.     struct IntuiMessage *msg;
  239.     BOOL done = FALSE;
  240.     BOOL press=FALSE;
  241.     int    key,id;
  242. /*        printf("tkExec\n");*/
  243.  
  244.     if (ReshapeFunc)
  245.         {
  246.         (*ReshapeFunc)(win.width,win.height);
  247.         }
  248.  
  249.     if (DisplayFunc)
  250.         {
  251.         (*DisplayFunc)();
  252.         }
  253.  
  254.  
  255.     while (!done)
  256.         {
  257.             
  258.         while ( (! done) && (msg = (struct IntuiMessage *)GetMsg(tkhwnd->UserPort)))
  259.             {
  260.                                 /* use a switch statement if looking for multiple event types */
  261.             id=msg->Class;
  262.         /*    printf("msg->Class=0x%x\n",id);*/
  263.             ReplyMsg((struct Message *)msg);
  264.             switch (id)
  265.                 {
  266.                 case IDCMP_NEWSIZE:
  267.                     if (ReshapeFunc)
  268.                         (*ReshapeFunc)(win.width,win.height);
  269.                     if (DisplayFunc)
  270.                         (*DisplayFunc)();
  271.                     break;
  272.                 case IDCMP_RAWKEY:
  273.                     key= atk_FixKeyRAW(msg->Code);
  274.                     if (key && KeyDownFunc)
  275.                         {
  276.                         GLenum mask;
  277.                         mask = 0;
  278.                         /*
  279.                         if (current.xkey.state & ControlMask)
  280.                             mask |= TK_CONTROL;  /*TODO Fixa ctrl och shift check */
  281.                         if (current.xkey.state & ShiftMask)
  282.                             mask |= TK_SHIFT;
  283.                         */ 
  284.                         (*KeyDownFunc)(key, mask);
  285.                         }
  286.                     break;
  287.                 case IDCMP_VANILLAKEY:
  288.                     if (KeyDownFunc);
  289.                         {
  290.                         GLenum mask;
  291.                         mask = 0;
  292.                         /*
  293.                         if (current.xkey.state & ControlMask)
  294.                             mask |= TK_CONTROL;   /*TODO Fixa ctrl och shift check */
  295.                         if (current.xkey.state & ShiftMask)
  296.                             mask |= TK_SHIFT;
  297.                         */
  298.                         (*KeyDownFunc)(msg->Code, mask);
  299.                         }
  300.                     break;
  301.                 case IDCMP_MOUSEMOVE:
  302.                     if (MouseMoveFunc)
  303.                         (*MouseMoveFunc)(msg->MouseX,msg->MouseY,press);
  304.                     break;                    
  305.                 case IDCMP_MOUSEBUTTONS:
  306.                 printf("code=%x\n",(WORD)msg->Code);
  307.                     if (!((WORD)msg->Code & 0x80))  // Mouse Press  (hack)
  308.                         {
  309.                         press=TK_LEFTBUTTON;
  310.                         if (MouseDownFunc)
  311.                             {
  312.                             GLenum mask;
  313.                             mask = 0;    /* TODO check right & middlebutton */
  314.                            mask = TK_LEFTBUTTON;
  315.                             /*
  316.                             if (current.xbutton.button == 1) { mask |= TK_LEFTBUTTON; }
  317.                             if (current.xbutton.button == 2) { mask |= TK_MIDDLEBUTTON;}
  318.                             if (current.xbutton.button == 3) { mask |= TK_RIGHTBUTTON;}
  319.                             */
  320.                             (*MouseDownFunc)(msg->MouseX,msg->MouseY,mask);
  321.                             }
  322.                         }
  323.                     else
  324.                         {
  325.                         if (MouseUpFunc)
  326.                             {
  327.                             GLenum mask;
  328.                             mask = 0;
  329.                             mask |= TK_LEFTBUTTON;
  330.                             /*
  331.                             if (current.xbutton.button == 1) { mask |= TK_LEFTBUTTON; }
  332.                             if (current.xbutton.button == 2) { mask |= TK_MIDDLEBUTTON;}
  333.                             if (current.xbutton.button == 3) { mask |= TK_RIGHTBUTTON;}
  334.                             */
  335.                             (*MouseUpFunc)(msg->MouseX,msg->MouseY,mask);
  336.                             }
  337.                         press=0;
  338.                         }
  339.                     break;
  340.                 case IDCMP_CLOSEWINDOW:
  341.                         done = TRUE;
  342.                         break;
  343.                     }
  344.                                 /* TODO Fill this with tests and call apropriate functions */
  345.                                 /* TODO (*ExposeFunc)(int, int) ??????? What should it do? */
  346.                 }
  347.         if(!done)
  348.             {
  349.             if (IdleFunc)
  350.                 {
  351.                 (*IdleFunc)();
  352.                 if (DisplayFunc)
  353.                     {
  354.                     (*DisplayFunc)();
  355.                     }
  356.                 }
  357.             else
  358.                 {
  359.                 WaitPort(tkhwnd->UserPort);
  360.                 }
  361.             }
  362.         }
  363.     /*    tkCloseWindow();*/
  364.     tkQuit();      /* if something else is neaded freeing*/
  365. }
  366.  
  367.  
  368. void tkExposeFunc(void (*Func)(int, int))
  369.     {
  370. /*    printf("ExposeFunc\n");*/
  371.     ExposeFunc = Func;
  372.     atk_modyfyIDCMP();
  373.     }
  374.  
  375. void tkReshapeFunc(void (*Func)(GLsizei, GLsizei))
  376.     {
  377. /*    printf("ReshapeFunc\n");*/
  378.     ReshapeFunc = Func;
  379.     atk_modyfyIDCMP();
  380.     }
  381.  
  382. void tkDisplayFunc(void (*Func)(void))
  383.     {
  384. /*    printf("DisplayFunc\n");*/
  385.     DisplayFunc = Func;
  386.     }
  387.  
  388. void tkKeyDownFunc(GLenum (*Func)(int, GLenum))
  389.     {
  390. /*    printf("KeyDownFunc\n");*/
  391.     KeyDownFunc = Func;
  392.     atk_modyfyIDCMP();
  393.     }
  394.  
  395. void tkMouseDownFunc(GLenum (*Func)(int, int, GLenum))
  396.     {
  397. /*    printf("MouseDownFunc\n");*/
  398.     MouseDownFunc = Func;
  399.     atk_modyfyIDCMP();
  400.     }
  401.  
  402. void tkMouseUpFunc(GLenum (*Func)(int, int, GLenum))
  403.     {
  404. /*    printf("MouseUpFunc\n");*/
  405.     MouseUpFunc = Func;
  406.     atk_modyfyIDCMP();
  407.     }
  408.  
  409. void tkMouseMoveFunc(GLenum (*Func)(int, int, GLenum))
  410.     {
  411. /*    printf("MouseMoveFunc\n");*/
  412.     MouseMoveFunc = Func;
  413.     atk_modyfyIDCMP();
  414.     }
  415.  
  416. void tkIdleFunc(void (*Func)(void))
  417.     {
  418. /*    printf("IdleFunc\n");*/
  419.     IdleFunc = Func;
  420.     }
  421.  
  422. void tkInitPosition(int x, int y, int width, int height)
  423.     {
  424.     win.x=x;
  425.     win.y=y;
  426.     win.width=width;
  427.     win.height=height;
  428.     }
  429.  
  430. void tkInitDisplayMode(GLenum type)
  431.     {
  432.     win.type = type;
  433.     }
  434.  
  435. GLenum tkInitWindow(char *title)
  436.     {
  437.     GLenum   Result = GL_FALSE,
  438.                     RGB_Flag=GL_TRUE,
  439.                     DB_Flag=GL_FALSE;
  440.  
  441.  
  442.     if (IntuitionBase = OpenLibrary("intuition.library",37))
  443.         {
  444.         if (GfxBase = OpenLibrary("graphics.library",37))
  445.             {
  446.             if (!(screen = LockPubScreen("MESA")))
  447.                 screen = LockPubScreen(NULL);
  448.             if (screen)
  449.                 {
  450.                                             /* open the window on the public screen */
  451.  
  452.  
  453.                 tkhwnd = OpenWindowTags(NULL,
  454.                             WA_Left,  win.x,            WA_Top,    win.x,
  455.                             WA_InnerWidth, win.width,    WA_InnerHeight, win.height,
  456.                             WA_DragBar,                TRUE,
  457.                             WA_CloseGadget,        TRUE,
  458.                             WA_SizeGadget,            TRUE,
  459.                             WA_DepthGadget,        TRUE,
  460.                             WA_SmartRefresh,        TRUE,
  461.                             WA_ReportMouse,        TRUE,
  462.                             WA_NoCareRefresh,        TRUE,
  463.                             WA_SizeBBottom,        TRUE,
  464.                             WA_MinWidth,100,        WA_MinHeight,30,
  465.                             WA_MaxWidth,-1,        WA_MaxHeight,-1,
  466.                             WA_IDCMP,                atk_setIDCMPs(),
  467.                             WA_Flags,                WFLG_SIZEGADGET | WFLG_DRAGBAR,
  468.                             WA_Title,                title,
  469.                             WA_PubScreen,            screen,
  470.                             TAG_END);
  471.                                             /* Unlock the screen.  The window now acts as a lock on
  472.                                             ** the screen, and we do not need the screen after the
  473.                                             ** window has been closed.  */
  474.                 UnlockPubScreen(NULL, screen);
  475.                 if (tkhwnd)
  476.                     {
  477.                     if (win.type & TK_INDEX)
  478.                         {
  479.                         RGB_Flag=GL_FALSE;
  480.                         }
  481.                     if (win.type & TK_DOUBLE)
  482.                         {
  483.                         DB_Flag=GL_TRUE;
  484.                         }
  485.  
  486.                     win.context=AmigaMesaCreateContext(tkhwnd,RGB_Flag,DB_Flag);
  487.                 AmigaMesaMakeCurrent(win.context);
  488.                 return GL_TRUE;
  489.                     
  490.                     }
  491.                 else
  492.                     {
  493.                     printf("Failed to open window.\n");
  494.                     return(Result);
  495.                     }
  496.                 }
  497.             }    
  498.         }
  499. }
  500.  
  501.  
  502.  
  503. /******************************************************************************/
  504.  
  505. /*
  506.  * You cannot just call DestroyWindow() here.  The programs do not expect
  507.  * tkQuit() to return;  DestroyWindow() just sends a WM_DESTROY message
  508.  */
  509.  
  510. void tkQuit(void)
  511. {
  512.     tkCloseWindow();
  513.    exit(0);    /*TODO*/
  514. }
  515.  
  516. /******************************************************************************/
  517.  
  518. void tkSetOneColor(int index, float r, float g, float b)
  519. {
  520.     win.context->penconv[index]=RGBA(r*255,g*255,b*255,255);
  521. }
  522.  
  523. void tkSetFogRamp(int density, int startIndex)
  524. {
  525. /* TODO */
  526. printf("tkSetFogRamp(%d,%d) TODO\n",density,startIndex);
  527. }
  528.  
  529. void tkSetGreyRamp(void)
  530. {
  531.  /* TODO */
  532. printf("tkSetFogRamp() TODO\n");
  533.  
  534. }
  535.  
  536. void tkSetRGBMap( int Size, float *Values )
  537. {
  538. /* TODO */
  539. printf("tkSetRGBMap(%d,%f) TODO\n",Size,*Values);
  540. }
  541.  
  542. void tkSetOverlayMap(int size, float *rgb)
  543. {
  544. printf("tkSetOverlayMap(%d,%f) TODO\n",size,rgb);
  545. }
  546. /******************************************************************************/
  547.  
  548. void tkSwapBuffers(void)
  549. {
  550.   AmigaMesaSwapBuffers();
  551. }
  552.  
  553. /******************************************************************************/
  554.  
  555. GLint tkGetColorMapSize(void)
  556. {
  557.  /* TODO */
  558.     printf("tkGetColorMapSize() TODO\n");
  559.     return(255);
  560. }
  561.  
  562. void tkGetMouseLoc(int *x, int *y)
  563. {
  564.     *x = tkhwnd->MouseX;
  565.     *y = tkhwnd->MouseY;
  566. }
  567.  
  568.  
  569. GLenum tkGetDisplayMode(void)
  570. {
  571.     return win.type;
  572. }
  573.  
  574. GLenum tkSetWindowLevel(GLenum level)
  575. {
  576. printf("tkSetWindowLevel(%d) TODO\n",level);
  577. }
  578.  
  579. /*
  580. TK_RGBImageRec *tkRGBImageLoad(char *fileName)
  581. {
  582. printf("tkRGBImageLoad(%s) TODO\n",fileName);
  583. }
  584. */
  585.  
  586. void tkGetSystem(TKenum type, void *ptr)
  587. {
  588. printf("tkGetSystem(%d,*ptr) TODO\n",type);
  589. /* getset.c */
  590. }
  591.